package com.me.myFramework.aop22.aop124;

import com.me.myFramework.aop22.IBuy;
import com.me.myFramework.aop22.Switch;
import com.me.myFramework.aop22.aop123.intercept.method.ReflectiveMethodInvocation123;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ReflectiveMethodInvocation;

import java.lang.reflect.Method;

/**
 * 《AOP-124》：  <br><br>
 *
 * TODO 以下变更点：
 *      使用 Spring ReflectiveMethodInvocation 替换自定义的 {@link ReflectiveMethodInvocation123 ReflectiveMethodInvocation}。
 *      由于 {@link ReflectiveMethodInvocation Spring ReflectiveMethodInvocation} 构造函数是 protected，所以通过反射实现
 *
 * @author ME
 */
public class MyAop124 {

    /**
     * 通过 模板模式 提取增强逻辑：方法执行前、正常返回、异常返回
     */
    public static void main(String[] args) throws Throwable {
        IBuy target = new Switch();

        ProxyFactory124 proxyFactory = new ProxyFactory124(target);
        proxyFactory.addAdvice(new MethodBeforeAdvice() {
            @Override
            public void before(Method method, Object[] args, Object target) throws Throwable {
                System.out.println("[AOP1] 就想看看谁买东西了：" + args[0]);
            }
        });
        proxyFactory.addAdvice(new AfterReturningAdvice() {
            @Override
            public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
                System.out.println("[AOP2] 记录购买价格 " + returnValue);
            }
        });
        IBuy proxy = (IBuy) proxyFactory.getProxy();
        proxy.buy("Tom");
    }

}
